/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itschapala.dsw2.controlcobros.vista;

import java.io.ByteArrayInputStream;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import mx.itschapala.dsw2.controlcobros.Bl.DocumentacionBlLocal;
import mx.itschapala.dsw2.controlcobros.Bl.UsuarioBlLocal;
import mx.itschapala.dsw2.controlcobros.modelo.Documentacion;
import mx.itschapala.dsw2.controlcobros.modelo.Usuario;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author Maricela
 */
@ManagedBean
@SessionScoped
public class DocumentBean {
    //---------- EJB--------------------
    @EJB
    private UsuarioBlLocal usuarioBl;
    @EJB
    private DocumentacionBlLocal documentacionBl;
    //---------- Variables---------------
    private List<Documentacion> lista;
    private List<Usuario> listaUsuario;
    private Documentacion document;
    private int idUsuarioSeleccionado;
    private String accion;
    
    private StreamedContent img1;
    private UploadedFile file1;
    
    private StreamedContent img;
    private UploadedFile file;
    
    private StreamedContent img2;
    private UploadedFile file2;
    
    private StreamedContent img3;
    private UploadedFile file3;
    
    private StreamedContent img4;
    private UploadedFile file4;
//---------- Constructor------------

    public DocumentBean() {
        document = new Documentacion ();
    }
 
     ///--------- Metodos Get y Set------- 

    public UploadedFile getFile2() {
        return file2;
    }

    public void setFile2(UploadedFile file2) {
        this.file2 = file2;
    }

    public UploadedFile getFile3() {
        return file3;
    }

    public void setFile3(UploadedFile file3) {
        this.file3 = file3;
    }

    public UploadedFile getFile4() {
        return file4;
    }

    public void setFile4(UploadedFile file4) {
        this.file4 = file4;
    }

    public StreamedContent getImg2() {
        if(document.getComprobanteDomicilio()!=null){
            img2 = new DefaultStreamedContent(new ByteArrayInputStream(document.getComprobanteDomicilio()));
        }
        return img2;
    }

    public StreamedContent getImg3() {
        if(document.getCurp()!=null){
            img3 = new DefaultStreamedContent(new ByteArrayInputStream(document.getCurp()));
        }
        return img3;
    }

    public StreamedContent getImg4() {
        if(document.getFotos()!=null){
            img4 = new DefaultStreamedContent(new ByteArrayInputStream(document.getFotos()));
        }
        return img4;
    }
    
    
    

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

    public StreamedContent getImg() {
        
        if(document.getActaNacimiento()!=null){
            img = new DefaultStreamedContent(new ByteArrayInputStream(document.getActaNacimiento()));
        }
        return img;
    }
    
    

    public UploadedFile getFile1() {
        return file1;
    }

    public void setFile1(UploadedFile file) {
        this.file1 = file;
    }

    public StreamedContent getImg1() {
        if(document.getCertificadoEstudios()!=null){
            img1 = new DefaultStreamedContent(new ByteArrayInputStream(document.getCertificadoEstudios()));
        }
        return img1;
    }
    
    
    
     public List<Usuario> getListUsuario() {
         listaUsuario=usuarioBl.getAll();
        return listaUsuario;
    }
     public List<Documentacion> getLista() {
        lista=documentacionBl.getAll();
        return lista;
    }

    public Documentacion getDocument() {
        return document;
    }

    public void setDocument(Documentacion document) {
        this.document = document;
    }
     public int getIdUsuarioSeleccionado() {
        return idUsuarioSeleccionado;
    }

    public void setIdUsuarioSeleccionado(int idUsuarioSeleccionado) {
        this.idUsuarioSeleccionado = idUsuarioSeleccionado;
    }
    //---------- Acciones----------------
    public String procesarAccion (){
    if (accion.equalsIgnoreCase("Nuevo")){
    documentacionBl.register(document);
    }else {
        if (accion.equalsIgnoreCase("Editar")){
    documentacionBl.modify(document);
    }else if (accion.equalsIgnoreCase("Eliminar")){
    documentacionBl.delete(document);
    }
    }
    accion ="";
    return "Documentacion";
    
    }
//---------- Listeners---------------
     public void prepararNuevo (ActionEvent evt){
    document= new Documentacion ();
    accion="Nuevo";
    }
    public void prepararEditar (ActionEvent evt){
//        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
//        System.out.println(index);
//        document = lista.get(index);
        accion = "Editar";
    }
    
    public void prepararEliminar (ActionEvent evt){
//        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
//        System.out.println(index);
//        document = lista.get(index);
        accion = "Eliminar";
    }  
    public String prepararUsuario (){
        document.setIdUsuario(usuarioBl.findById(idUsuarioSeleccionado));
        return "ConfirmarDocumentacion";
    }
    
    public void handleFileUpload1(FileUploadEvent event) {  
        FacesMessage msg1;
        try{
            System.out.println("OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK");
           document.setCertificadoEstudios(event.getFile().getContents()); 
            msg1 = new FacesMessage("Correcto",event.getFile().getFileName()+"imagen cargada!");
        }catch(Exception e){
            msg1 = new FacesMessage("Error",event.getFile().getFileName()+"no se subio");
        }
        FacesContext.getCurrentInstance().addMessage(null, msg1);
    }
    
    public void handleFileUpload(FileUploadEvent event) {  
        FacesMessage msg;
        try{
            System.out.println("OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK");
           document.setActaNacimiento(event.getFile().getContents()); 
            msg = new FacesMessage("Correcto",event.getFile().getFileName()+"imagen cargada!");
        }catch(Exception e){
            msg = new FacesMessage("Error",event.getFile().getFileName()+"no se subio");
        }
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    
    public void handleFileUpload2(FileUploadEvent event) {  
        FacesMessage msg2;
        try{
            System.out.println("OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK");
           document.setComprobanteDomicilio(event.getFile().getContents()); 
            msg2 = new FacesMessage("Correcto",event.getFile().getFileName()+"imagen cargada!");
        }catch(Exception e){
            msg2 = new FacesMessage("Error",event.getFile().getFileName()+"no se subio");
        }
        FacesContext.getCurrentInstance().addMessage(null, msg2);
    }
    
    public void handleFileUpload3(FileUploadEvent event) {  
        FacesMessage msg3;
        try{
            System.out.println("OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK");
           document.setCurp(event.getFile().getContents()); 
            msg3 = new FacesMessage("Correcto",event.getFile().getFileName()+"imagen cargada!");
        }catch(Exception e){
            msg3 = new FacesMessage("Error",event.getFile().getFileName()+"no se subio");
        }
        FacesContext.getCurrentInstance().addMessage(null, msg3);
    }
    
    public void handleFileUpload4(FileUploadEvent event) {  
        FacesMessage msg4;
        try{
            System.out.println("OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK");
           document.setFotos(event.getFile().getContents()); 
            msg4 = new FacesMessage("Correcto",event.getFile().getFileName()+"imagen cargada!");
        }catch(Exception e){
            msg4 = new FacesMessage("Error",event.getFile().getFileName()+"no se subio");
        }
        FacesContext.getCurrentInstance().addMessage(null, msg4);
    }
     
}
